<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品编号" prop="item1Id">
        <el-input
          v-model="queryParams.item1Id"
          placeholder="请输入商品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货物名称" prop="item1Name">
        <el-input
          v-model="queryParams.item1Name"
          placeholder="请输入货物名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类区域" prop="classArea">
        <el-input
          v-model="queryParams.classArea"
          placeholder="请输入分类区域"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态信息" prop="stateInfo">
        <el-input
          v-model="queryParams.stateInfo"
          placeholder="请输入状态信息"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:items:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:items:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:items:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:items:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-divider></el-divider>
    <el-descriptions title="商品样例信息">
      <el-descriptions-item label="货物名称">冰露</el-descriptions-item>
      <el-descriptions-item label="实物图片">
        <div class="demo-image">
          <div class="block" v-for="fit in fits" :key="fit">
            <span class="demonstration"></span>
            <el-image
              style="width: 80px; height: 80px"
              :src="url"
              :fit="fit"></el-image>
          </div>
        </div>
      </el-descriptions-item>
      <el-descriptions-item label="生产地">大连市</el-descriptions-item>
      <el-descriptions-item label="区域">
        <el-tag size="small">饮料区域</el-tag>
      </el-descriptions-item>
      <el-descriptions-item label="货物质量">合格品</el-descriptions-item>
    </el-descriptions>
    <el-divider></el-divider>
    <el-table v-loading="loading" :data="itemsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品编号" align="center" prop="item1Id" />
      <el-table-column label="货物名称" align="center" prop="item1Name"/> >
      <el-table-column label="货物价格" align="center" prop="itemPrice" />
      <el-table-column label="分类区域" align="center" prop="classArea" />
      <el-table-column label="规格" align="center" prop="item1Speci" />
      <el-table-column label="状态信息" align="center" prop="stateInfo" />
      <el-table-column label="库存容量" align="center" prop="invenCapa" >
        <el-progress :percentage="80" :color="customColors"></el-progress>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:items:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:items:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="货物名称" prop="item1Name">
          <el-input v-model="form.item1Name" placeholder="请输入货物名称" />
        </el-form-item>
        <el-form-item label="货物价格" prop="itemPrice">
          <el-input v-model="form.itemPrice" placeholder="请输入货物价格" />
        </el-form-item>
        <el-form-item label="分类区域" prop="classArea">
          <el-input v-model="form.classArea" placeholder="请输入分类区域" />
        </el-form-item>
        <el-form-item label="规格" prop="item1Speci">
          <el-input v-model="form.item1Speci" placeholder="请输入规格" />
        </el-form-item>
        <el-form-item label="状态信息" prop="stateInfo">
          <el-input v-model="form.stateInfo" placeholder="请输入状态信息" />
        </el-form-item>
        <el-form-item label="库存容量" prop="invenCapa">
          <el-input v-model="form.invenCapa" placeholder="请输入库存容量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listItems, getItems, delItems, addItems, updateItems } from "@/api/system/items";

export default {
  name: "Items",
  data() {
    return {
      fits: ['实物图片：'],
      url: 'https://p1.ssl.qhmsg.com/t0187294f4c1eb369b1.jpg',
      percentage: 60,
      customColors: [
        {color: '#f56c6c', percentage: 20},
        {color: '#e6a23c', percentage: 40},
        {color: '#5cb87a', percentage: 60},
        {color: '#1989fa', percentage: 80},
        {color: '#6f7ad3', percentage: 100}
      ],
      value4: 100,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品信息表格数据
      itemsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        item1Id: null,
        item1Name: null,
        classArea: null,
        stateInfo: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        item1Id: [
          { required: true, message: "商品编号不能为空", trigger: "blur" }
        ],
        item1Name: [
          { required: true, message: "货物名称不能为空", trigger: "blur" }
        ],
        itemPrice: [
          { required: true, message: "货物价格不能为空", trigger: "blur" }
        ],
        classArea: [
          { required: true, message: "分类区域不能为空", trigger: "blur" }
        ],
        item1Speci: [
          { required: true, message: "规格不能为空", trigger: "blur" }
        ],
        stateInfo: [
          { required: true, message: "状态信息不能为空", trigger: "blur" }
        ],
        invenCapa: [
          { required: true, message: "库存容量不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    formatTooltip(val) {
      return val / 100;
    },
    format(percentage) {
      return percentage === 100 ? '满' : `${percentage}%`;
    },
    /** 查询商品信息列表 */
    getList() {
      this.loading = true;
      listItems(this.queryParams).then(response => {
        this.itemsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        item1Id: null,
        item1Name: null,
        itemPrice: null,
        classArea: null,
        item1Speci: null,
        stateInfo: null,
        invenCapa: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.item1Id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const item1Id = row.item1Id || this.ids
      getItems(item1Id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.item1Id != null) {
            updateItems(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addItems(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const item1Ids = row.item1Id || this.ids;
      this.$modal.confirm('是否确认删除商品信息编号为"' + item1Ids + '"的数据项？').then(function() {
        return delItems(item1Ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/items/export', {
        ...this.queryParams
      }, `items_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
